package com.employeemanage.controller;

import com.employeemanage.domain.resp.PageResult;
import com.employeemanage.domain.resp.Result;
import com.employeemanage.domain.bean.TblEmployee;
import com.employeemanage.domain.vo.EmployeeAddVo;
import com.employeemanage.domain.vo.EmployeeListVo;
import com.employeemanage.domain.vo.EmployeeUpdateVo;
import com.employeemanage.domain.vo.EmployeePerformanceListVo;
import com.employeemanage.domain.vo.EmployeePerformanceUpdateVo;
import com.employeemanage.domain.vo.EmployeePerformanceResponseVo;
import com.employeemanage.service.TblEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/employee")
public class TblEmployeeController {

    @Autowired
    private TblEmployeeService employeeService;

    /**
     * 获取员工列表
     * @param employeeListVo 查询条件
     * @return 员工列表
     */
    @PostMapping("/list")
    public Result<PageResult<TblEmployee>> getEmployeeList(@RequestBody EmployeeListVo employeeListVo) {
        try {
            PageResult<TblEmployee> result = employeeService.getEmployeeList(employeeListVo);
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail("获取员工列表失败：" + e.getMessage());
        }
    }

    /**
     * 新增员工
     * @param employeeAddVo 员工信息
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result<Map<String, Object>> addEmployee(@RequestBody EmployeeAddVo employeeAddVo) {
        try {
            Long employeeId = employeeService.addEmployee(employeeAddVo);
            Map<String, Object> data = new HashMap<>();
            data.put("id", employeeId);
            return Result.success(data);
        } catch (Exception e) {
            return Result.fail("新增员工失败：" + e.getMessage());
        }
    }

    /**
     * 更新员工信息
     * @param employeeUpdateVo 员工信息
     * @return 更新结果
     */
    @PostMapping("/update")
    public Result<String> updateEmployee(@RequestBody EmployeeUpdateVo employeeUpdateVo) {
        try {
            boolean success = employeeService.updateEmployee(employeeUpdateVo);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.fail("员工不存在");
            }
        } catch (Exception e) {
            return Result.fail("更新员工信息失败：" + e.getMessage());
        }
    }

    /**
     * 删除员工
     * @param request 包含员工ID的请求
     * @return 删除结果
     */
    @PostMapping("/delete")
    public Result<String> deleteEmployee(@RequestBody Map<String, Long> request) {
        try {
            Long id = request.get("id");
            if (id == null) {
                return Result.fail("员工ID不能为空");
            }
            
            boolean success = employeeService.deleteEmployee(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.fail("员工不存在");
            }
        } catch (Exception e) {
            return Result.fail("删除员工失败：" + e.getMessage());
        }
    }

    /**
     * 获取员工绩效列表
     * @param performanceListVo 查询条件
     * @return 员工绩效列表
     */
    @PostMapping("/performance/list")
    public Result<PageResult<EmployeePerformanceResponseVo>> getEmployeePerformanceList(@RequestBody EmployeePerformanceListVo performanceListVo) {
        try {
            PageResult<EmployeePerformanceResponseVo> result = employeeService.getEmployeePerformanceList(performanceListVo);
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail("获取员工绩效列表失败：" + e.getMessage());
        }
    }

    /**
     * 更新员工绩效
     * @param performanceUpdateVo 绩效信息
     * @return 更新结果
     */
    @PostMapping("/performance/update")
    public Result<String> updateEmployeePerformance(@RequestBody EmployeePerformanceUpdateVo performanceUpdateVo) {
        try {
            boolean success = employeeService.updateEmployeePerformance(performanceUpdateVo);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.fail("员工不存在");
            }
        } catch (Exception e) {
            return Result.fail("更新员工绩效失败：" + e.getMessage());
        }
    }

    /**
     * 删除员工绩效（重置为0）
     * @param request 包含员工ID的请求
     * @return 删除结果
     */
    @PostMapping("/performance/delete")
    public Result<String> deleteEmployeePerformance(@RequestBody Map<String, Long> request) {
        try {
            Long id = request.get("id");
            if (id == null) {
                return Result.fail("员工ID不能为空");
            }
            
            boolean success = employeeService.deleteEmployeePerformance(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.fail("员工不存在");
            }
        } catch (Exception e) {
            return Result.fail("删除员工绩效失败：" + e.getMessage());
        }
    }
}
